﻿//199. 二叉树的右视图
//给定一个二叉树的 根节点 root，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root)
    {
        vector<int> ans;
        if (!root)
            return ans;
        queue<pair<TreeNode*, int>> que;
        que.push({ root,0 });
        while (!que.empty())
        {
            TreeNode* node = que.front().first;
            int depth = que.front().second;
            que.pop();
            if (que.empty() || depth != que.front().second)
                ans.push_back(node->val);
            if (node->left)
                que.push({ node->left,depth + 1 });
            if (node->right)
                que.push({ node->right,depth + 1 });
        }
        return ans;
    }
};